package pri.black.dogview.service;

import org.springframework.stereotype.Service;
import pri.black.dogview.domain.DeviceGpsLog;
import pri.black.dogview.domain.R;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DriverService {
//
//    private double acc(List<DeviceGpsLog> gpslogs, int current) {
//        DeviceGpsLog deviceGpsLog = gpslogs.get(current);
//        LocalDateTime createTime = deviceGpsLog.getCreateTime();
//        int iterator = current;
//        if (iterator == 0) {
//            LocalDateTime first = createTime;
//            LocalDateTime last = createTime;
//            for (int j = iterator; Duration.between(first,last).getSeconds() > 5 && j < gpslogs.size(); j++) {
//                DeviceGpsLog curr = gpslogs.get(j);
//                LocalDateTime currCreateTime = curr.getCreateTime();
//                if (!currCreateTime.isAfter(first)) {
//                    first = currCreateTime;
//                }
//                if (!currCreateTime.isBefore(last)) {
//                    last = currCreateTime;
//                }
//            }
//            return
//        }
//    }

    public R doIt(List<DeviceGpsLog> gpslogs) {
        long seconds = 0;
        double totalLen = 0 ;

        int overspeedTimes = 0;
        boolean lastOver = false;


        int alertTime = 0;
        int fastAdd  = 0;
        long highSpeed = 0;
        long lowSpeed = 0;
        for (int i = 1; i < gpslogs.size(); i++) {
            DeviceGpsLog log = gpslogs.get(i);
            if (log.getSpeed() != null ) {
                Duration between = Duration.between(gpslogs.get(i - 1).getCreateTime(), log.getCreateTime());
                if ( log.getSpeed()== 0) {
                    seconds += between.getSeconds();
                }else {
                    Integer lastSpeed = gpslogs.get(i - 1).getSpeed();
                    if (lastSpeed != null) {
                        totalLen += (log.getSpeed() + lastSpeed)  / 2.0  * between.getSeconds(); // km/h * s = km / 3600s * s = km / 3600
                        if ((log.getSpeed() + lastSpeed) / 2.0 > 30) {
                            highSpeed += between.getSeconds();
                        }else {
                            lowSpeed += between.getSeconds();
                        }
                    }
                }
                if (log.getSpeed() > 80) {
                    if (lastOver) {

                    }else {
                        lastOver = true;
                        overspeedTimes += 1;
                    }
                }else {
                    if (lastOver) {
                        lastOver = false;
                    }else {

                    }
                }
                Integer lastSpeed = gpslogs.get(i - 1).getSpeed();
                if (lastSpeed != null) {
                    Integer currentSpeed = log.getSpeed();
                    if (between.getSeconds() == 0) {
                        log.setAcc("0");
                    }else {
                        double speedAcc = (currentSpeed - lastSpeed) * 1.0 / between.getSeconds();
                        log.setAcc(String.format("%.2f", speedAcc));
                        if (speedAcc < -30 * 1.0) {
                            alertTime += 1;
                        } else if (speedAcc > 30 * 1.0) {
                            fastAdd += 1;
                        }
                    }
                }

            }

            log.setMileage(totalLen/ 3600);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("logs", gpslogs);
        data.put("highSpeed", highSpeed / 60);
        data.put("lowSpeed", lowSpeed / 60);
        data.put("overspeedTimes", overspeedTimes);
        data.put("alertTime", alertTime);
        data.put("fastAdd", fastAdd);
        data.put("seconds", seconds);
        data.put("minute", seconds / 60);
        data.put("length", String.format("%.3f", totalLen / 3600));
        return R.ok(data);
    }
}
